package kin.system.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreemarkerUtil {
public static void build(String templateNmae,String[] fullName){
	/*if(templateEncoding==null){
		templateEncoding="utf-8";
	}*/
	try{
	Configuration cfg = new Configuration();
	cfg.setDirectoryForTemplateLoading(new File(ClassLoader.getSystemResource(".").getPath()+"\\kin\\ftl"));
	 Template managerTpl = cfg.getTemplate(templateNmae+".ftl","utf-8");
	 Map root = new HashMap(); 
		for(int i=0;i<fullName.length;i++){
			String full=fullName[i];
			
			String simple = full.substring(full.lastIndexOf(".")+1,full.length());
			
			root.put("simple",simple);
			root.put("full", full);
			String path=null;
			if("ManagerIMP".equals(templateNmae)){
				path="dao";
			}else{
				path=templateNmae.toLowerCase();
			}
			File file=new File(System.getProperty("user.dir")+"\\src\\kin\\"+path+"\\"+simple+templateNmae+".java");
			Writer write = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
			managerTpl.process(root, write);
			write.flush();
			
		
			
		}
	 
}catch(Exception e){
	

}
}}